package com.sensetime.bi.senselink.server.util;

import com.google.common.collect.Lists;
import com.sensetime.bi.senselink.server.constant.Constant;
import io.netty.handler.codec.rtsp.RtspHeaderNames;
import io.netty.handler.codec.rtsp.RtspHeaders;
import io.netty.handler.codec.rtsp.RtspMethods;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class StringUtils {
    public static final char SPACE = ' ';
    public static String[] getLines (String msg) {

        StringTokenizer tokens = new StringTokenizer(msg, Constant.CRLF);
        int lineCount = tokens.countTokens();
        String[] lines = new String[lineCount];

        int i = 0;
        while (tokens.hasMoreTokens()) {
            lines[i++] = tokens.nextToken();
        }
        return lines;
    }

    public static String[] getTokens (String str, String splitStr) {

        StringTokenizer tokens = new StringTokenizer(str, splitStr);
        int count = tokens.countTokens();
        String[] strs = new String[count];

        int i = 0;
        while (tokens.hasMoreTokens()) {
            strs[i++] = tokens.nextToken();
        }
        return strs;
    }

    // For our byte to hex conversion routine
    final static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes, int length) {
        char[] hexChars = new char[length * 2];
        for ( int j = 0; j < length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static String byteToHex(byte onebyte) {
        char[] hexChars = new char[2];
        int v = onebyte & 0xFF;
        hexChars[0] = hexArray[v >>> 4];
        hexChars[1] = hexArray[v & 0x0F];
        return new String(hexChars);
    }



    /**
     * 获取第{#rank} 个目标字符在源字符串中的位置
     * @param ori 源字符串
     * @param des 目标字符
     * @param rank 排名位次
     * @return 位置
     */
    public static int getPos(String ori, char des, int rank) {
        int idx=0, num=0;
        for (char ch : ori.toCharArray()) {
            if (ch == des) {
                num++;
            }
            idx++;
            if (num == rank) {
                break;
            }
        }
        if (num < rank) {
            return -1;
        }
        return idx;
    }

    public static String[] getRawLines(String request) {
        List<String> list = Lists.newArrayList();
        char[] chars = request.toCharArray();
        int length = chars.length;
        int cur = 0;
        for (int i=0; i < length-1; i++) {
            if (chars[i] == '\r' && chars[i+1] == '\n' && i != cur) {
                char[] destChar = new char[i-cur];
                System.arraycopy(chars, cur, destChar, 0,i-cur);
                list.add(new String(destChar));
                list.add(Constant.CRLF);
                cur = i + 2;
                i++;
            }
        }
        if (cur < length) {
            char[] destChar = new char[length-cur];
            System.arraycopy(chars, cur, destChar, 0,length-cur);
            list.add(new String(destChar));
        }
        String[] lines = new String[list.size()];
        list.toArray(lines);
        return lines;
    }

    public static void main(String[] args) {
        String msg = " OPTIONS rtsp://127.0.0.1:554/test.mp4 RTSP/1.0\r\n" +
                "CSeq: 1\r\n" +
                "Content-Length: 0\r\n\r\n";
        String[] rawLines = getRawLines(msg);
        for (String str : rawLines) {
            System.out.println("[" + str + "]");
        }

    }


}
